package KC.utils;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Vector;

/**
 * <p>Title: Guarderias Net</p>
 * <p>Description: Sistema de Guarderias Monitoreadas Con WebCams</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Gerinnov</p>
 * @author Jose de Jesus Clodoaldo Delgado Rodriguez
 * @version 1.0
 */

public class Mail {
  /**
   * Mail Host
   */
  private String host = "";
  /**
   * Mail Recipient
   */
  private Vector mailTo = new Vector();
  /**
   * Mail Recipient CC
   */
  private Vector mailCC = new Vector();
  /**
   * Mail Sender
   */
  private String mailFrom = "";
  /**
   * Mail To authentication
   */
  private String mailAut = "";
  /**
   * Its necesary if authentication is necesary to
   */
  private String passwordFrom = "";
  /**
   * Message Subject
   */
  private String subject = "";
  /**
   * Message Body
   */
  private String body = "";
  /**
   * Message Authentication
   */
  private boolean authentication = false;
  /**
   * Sets Host Property
   */
  public void setHost( String host ){
    this.host = host;
  }
  /**
   * Gets Host Property
   * @return Host Property
   */
  public String getHost( ){
    return host;
  }
  /**
   * Add Mail To Property
   */
  public void addMailTo( String mailTo ){
    this.mailTo.add(mailTo);
  }
  /**
   * Remove mail to mails to
   * @param mailTo
   */
  public void removeMailTo( String mailTo ){
    int index = this.mailTo.indexOf(mailTo);
    if( index > -1 ){
      this.mailTo.remove(index);
    }
  }
  /**
   * Gets Mails To Property
   * @return Mails To Property String objects
   */
  public Object[] getMailTo( ){
    return mailTo.toArray();
  }
  /**
   * Add Mail CC Property
   */
  public void addMailCC( String mailCC ){
    this.mailCC.add(mailCC);
  }
  /**
   * Remove mail to mails CC
   * @param mailTo
   */
  public void removeMailCC( String mailCC ){
    int index = this.mailCC.indexOf(mailCC);
    if( index > -1 ){
      this.mailCC.remove(index);
    }
  }
  /**
   * Gets Mails CC Property
   * @return Mails To Property String objects
   */
  public Object[] getMailCC( ){
    return mailCC.toArray();
  }
  /**
   * Sets Mail From Property
   */
  public void setMailFrom( String mailFrom ){
    this.mailFrom = mailFrom;
  }
  /**
   * Gets Mail From Property
   * @return Mail From Property
   */
  public String getMailFrom( ){
    return mailFrom;
  }
  /**
   * Sets Mail Aut Property
   */
  public void setMailAut( String mailAut ){
    this.mailAut = mailAut;
  }
  /**
   * Gets Mail aut Property
   * @return Mail aut Property
   */
  public String getMailAut( ){
    return mailAut;
  }
  /**
   * Sets Password From Property
   */
  public void setPasswordFrom( String passwordFrom ){
    this.passwordFrom = passwordFrom;
  }
  /**
   * Gets Password From Property
   * @return Password From Property
   */
  public String getPasswordFrom( ){
    return passwordFrom;
  }
  /**
   * Sets Authentication Property
   */
  public void setAuthentication( boolean authentication ){
    this.authentication = authentication;
  }
  /**
   * Gets Authentication Property
   * @return Authentication Property
   */
  public boolean getAuthentication( ){
    return authentication;
  }
  /**
   * Sets Subject Property
   */
  public void setSubject( String subject ){
    this.subject = subject;
  }
  /**
   * Gets Subject Property
   * @return Subject Property
   */
  public String getSubject( ){
    return subject;
  }
  /**
   * Sets Body Property
   */
  public void setBody( String body ){
    this.body = body;
  }
  /**
   * Gets Body Property
   * @return Body Property
   */
  public String getBody( ){
    return body;
  }
  /**
   * Default constructor
   */
  public Mail( ){

  }
  /**
   * Send Mail
   * @return if sent mail Succesful Message, Error Message otherwise
   */
  public String sendMail( ){
    if( body != null && subject != null && passwordFrom != null && mailFrom!=null && mailTo != null && host != null && !body.equals("") && !subject.equals("") && !passwordFrom.equals("") && !mailFrom.equals("") && !mailTo.equals("") && !host.equals("") ){
      try{
        // Get system properties
        Properties props = System.getProperties();
        // Setup mail server
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", new Boolean( authentication ).toString() );
        // Get session
        Session sesion;
        if( authentication ){
          sesion = Session.getInstance(props, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication( mailAut, passwordFrom );
            }
          }
          );
        }else{
          sesion = null;
        }
        // Define message
        Message message = new MimeMessage(sesion);
        message.setHeader("Content-Type", "text/html");
        message.setFrom(new InternetAddress(mailFrom +"<"+mailFrom+">"));
        InternetAddress[] ReplyAddress = {new InternetAddress(mailFrom)};
        message.setReplyTo(ReplyAddress);
        if( mailTo != null ){
          for( int i = 0; i < mailTo.size(); i++ ){
            message.addRecipient(Message.RecipientType.TO,
                                 new InternetAddress((String)mailTo.get(i)));
          }
        }
        if( mailCC != null ){
          for( int i = 0; i < mailCC.size(); i++ ){
            message.addRecipient(Message.RecipientType.CC,
                                 new InternetAddress((String)mailCC.get(i)));
          }
        }
        message.setSubject(subject);
        // Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();
        // Fill the message
        messageBodyPart.setText(body);
        //Set the content type of message
        messageBodyPart.setHeader("Content-Type", "text/html");
        // Create a Multipart
        Multipart multipart = new MimeMultipart();
        // Add part one
        multipart.addBodyPart(messageBodyPart);
        // Put parts in message
        message.setContent(multipart);
        // Send the message
        Transport.send(message);
        return "";
      }catch( Exception ex ){
        return "Error: "+ex.getMessage();
      }
    }else{
      return "Error: Faltan Datos Para Enviar Mail";
    }
  }
}
